//
//  GoodsSelectViewController.m
//  JiuLe
//
//  Created by SunChao on 16/10/12.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "GoodsSelectViewController.h"
#import "SureOderViewController.h"
@interface GoodsSelectViewController ()
//@property (nonatomic,strong)NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIButton *reduceBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (nonatomic,assign)NSInteger maxNum;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

- (IBAction)reduceBtnClicked:(id)sender;
- (IBAction)addBtnClicked:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *scoreImg;

@end

@implementation GoodsSelectViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    //注册通知 商品信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:tongzhi object:nil];
    for (int i = 0; i<5; i++) {
        UIButton *button = [self.view viewWithTag:i+100];
        button.hidden =YES;
    }
    self.sureBtn.layer.cornerRadius = 25;
    [self.sureBtn setBackgroundColor:[UIColor orangeColor]];
    CALayer *layer = [self.imgView layer];
    layer.borderColor = [UIColor grayColor].CGColor;
    layer.borderWidth = 0.5f;
    self.imgView.image = [UIImage imageNamed:@"商城首页默认图片"];
    [self.addBtn setBackgroundImage:[UIImage imageNamed:@"add_disable"] forState:UIControlStateDisabled];
    [self.addBtn setBackgroundImage:[UIImage imageNamed:@"add_enable"] forState:UIControlStateNormal];
    [self.reduceBtn setBackgroundImage:[UIImage imageNamed:@"minus_enable"] forState:UIControlStateNormal];
    [self.reduceBtn setBackgroundImage:[UIImage imageNamed:@"minus_disable"] forState:UIControlStateDisabled];
}
- (void)tongzhi:(NSNotification *)text{
    if (text.userInfo == nil) {
        self.sureBtn.enabled =NO;
    }else{
    self.dataSource = [NSMutableArray new];
    self.dataSource = [text.userInfo objectForKey:@"userInfo"];
    NSString *price = [text.userInfo objectForKey:@"price"];
    NSString *title = [text.userInfo objectForKey:@"goodsTitle"];
    NSString *type = [text.userInfo objectForKey:@"type"];
    NSString *score = [text.userInfo objectForKey:@"score"];
    NSMutableArray *tmpArr = [NSMutableArray new];
    for (NSDictionary *dic in self.dataSource) {
        NSMutableDictionary *tmpDic = [NSMutableDictionary dictionaryWithDictionary:dic];
        [tmpDic setObject:price forKey:@"price"];
        [tmpDic setObject:title forKey:@"title"];
        [tmpDic setObject:score forKey:@"score"];
        [tmpDic setObject:type forKey:@"type"];
        [tmpArr addObject:tmpDic];
    }
    self.dataSource = tmpArr;
    [self createUIWithData:self.dataSource];
    }
}
- (void)createUIWithData:(NSArray *)array
{
    NSInteger num = self.dataSource.count;
    NSDictionary *dic =  [self.dataSource objectAtIndex:0];
    //显示金额
    if ([[dic objectForKey:@"type"]isEqualToString:@"3"]) {
        float price = [[dic objectForKey:@"price"]floatValue];
        self.priceLabel.text = [NSString stringWithFormat:@"%.0f",price];
        self.scoreImg.hidden =NO;
    }else
    {
        self.priceLabel.text = [NSString stringWithFormat:@"￥%@",[dic objectForKey:@"price"]];
        self.scoreImg.hidden = YES;
    }
    //显示样式
    for (int i = 0; i<num; i++) {
        UIButton *button = (UIButton *)[self.view viewWithTag:i+100];
        button.hidden =NO;
        NSString *stylename = [self.dataSource[i]objectForKey:@"stylename"];
        NSDictionary *dict =  [self.dataSource objectAtIndex:i];
        
        [button setTitle:stylename forState:UIControlStateNormal];
        [button setTitle:stylename forState:UIControlStateSelected];
        [button setTitle:stylename forState:UIControlStateDisabled];
        [button setTintColor:[UIColor whiteColor]];
        [button setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"3"] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateDisabled];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(colorSelected:) forControlEvents:UIControlEventTouchUpInside];
        //没有库存时按钮不允许被选中
        if ([[dict objectForKey:@"stockcount"]isEqualToString:@"0"]) {
            button.enabled =NO;
            self.addBtn.enabled =NO;
            self.reduceBtn.enabled = NO;
        }else
        {
            self.addBtn.enabled =YES;
            self.reduceBtn.enabled = YES;
        }
    }
    ////有库存时根据库存设置➕－按钮
    [self choiceButtonType];
    
}
- (void)choiceButtonType
{
    NSInteger num = self.dataSource.count;
    for (int i=0; i<num; i++) {
        NSDictionary *dict =  [self.dataSource objectAtIndex:i];
        int stockcount = [[dict objectForKey:@"stockcount"]intValue];
        
        if (!(stockcount==0)) {
            UIButton *button = (UIButton *)[self.view viewWithTag:i+100];
            button.selected =YES;
            _maxNum = [[dict objectForKey:@"stockcount"] integerValue];
            NSString *stylename = [dict objectForKey:@"styleimage"];
            [self.imgView sd_setImageWithURL:[NSURL URLWithString:stylename] placeholderImage:[UIImage imageNamed:@"商城首页默认图片"]];
            if (stockcount>1) {
                self.addBtn.enabled =YES;
                self.reduceBtn.enabled =NO;
            }else
            {
                self.addBtn.enabled =NO;
                self.reduceBtn.enabled =NO;
            }
            return;
        }
    }
}
- (void)colorSelected:(UIButton *)sender
{
    for (int i = 0; i<4; i++) {
        UIButton *button = [self.view viewWithTag:i+100];
        button.selected =NO;
        if (button.tag == sender.tag) {
            button.selected =YES;
            NSDictionary *dic = [self.dataSource objectAtIndex:i];
            NSString *stylename = [dic objectForKey:@"styleimage"];
            _maxNum = [[dic objectForKey:@"stockcount"] integerValue];
            [self.imgView sd_setImageWithURL:[NSURL URLWithString:stylename] placeholderImage:[UIImage imageNamed:@"商城首页默认图片"]];
            self.numLabel.text = @"1" ;
            if (_maxNum ==1) {
                self.addBtn.enabled =NO;
                self.reduceBtn.enabled =NO;
            }else
            {
                self.addBtn.enabled =YES;
                self.reduceBtn.enabled =NO;
            }
        }
    }
}
//点击确定跳转
- (IBAction)buttonClicked:(id)sender {
    NSInteger j = -1;
    for (int i = 0; i<4; i++) {
        UIButton *button = (UIButton *)[self.view viewWithTag:i+100];
        if (button.selected ==YES) {
            j = button.tag - 100;
        }
    }
    if (j ==-1) {
        [SVProgressHUD showErrorWithStatus:@"没有库存"];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary new];
    dic = [self.dataSource objectAtIndex:j];
    if ([[[self.dataSource objectAtIndex:0] objectForKey:@"type"]isEqualToString:@"3"]) {
        //积分支付
        NSDictionary *dic =  [self.dataSource objectAtIndex:j];
        NSString *numString = self.numLabel.text;
        NSMutableDictionary *tmpDic = [NSMutableDictionary new];
        [tmpDic setObject:dic forKey:@"goodsInfo"];
        [tmpDic setObject:numString forKey:@"number"];
        NSNotification *notification =[NSNotification notificationWithName:PushToScorePayVC object:nil userInfo:tmpDic];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        [self removeFromParentViewController];
        
    }else{
        [dic setObject:self.numLabel.text forKey:@"number"];
        NSNotification *notification =[NSNotification notificationWithName:sureIsClicked object:nil userInfo:dic];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}

#pragma mark - Table view data source


- (IBAction)reduceBtnClicked:(UIButton *)sender {
    
    int num = [self.numLabel.text intValue]-1;
    self.numLabel.text = [NSString stringWithFormat:@"%d",num];
    if (num<=1) {
        sender.enabled =NO;
    }else
    {
        self.addBtn.enabled =YES;
    }
}

- (IBAction)addBtnClicked:(id)sender {
    
    int num = [self.numLabel.text intValue]+1;
    self.numLabel.text = [NSString stringWithFormat:@"%d",num];
    if ([self.numLabel.text integerValue]>=_maxNum) {
        self.reduceBtn.enabled =YES;
        self.addBtn.enabled =NO;
    }else
    {
        self.addBtn.enabled =YES;
        self.reduceBtn.enabled =YES;
        
    }
    
}
@end
